//
//  MvvmViewController.m
//  ObjcGoNew
//
//  Created by 邢伟新 on 2021/4/21.
//

#import "MvvmViewController.h"
#import <Masonry/Masonry.h>
#import "LoginViewModel.h"
#import "MBProgressHUD+NJ.h"
#import "AppViewController.h"

#define marginLeftAndRight 15
#define textFiledHeight  45


@interface MvvmViewController ()

@property(nonatomic,strong)UIImageView *headerImageView;
@property(nonatomic,strong)UITextField *userNameTextfiled;
@property(nonatomic,strong)UITextField *PasswordTextfiled;
@property(nonatomic,strong)UIButton *loginButton;
@property(nonatomic,strong)UIImageView *bg_view;
@property(nonatomic,strong)LoginViewModel *viewModel;

@end

@implementation MvvmViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self _initNav];
    [self _initViews];
    [self _bindModels];
    
    
    
}
-(void)_bindModels{
    
    RAC(self.viewModel,userName) = [RACSignal merge:@[RACObserve(self.userNameTextfiled, text),self.userNameTextfiled.rac_textSignal]];
    RAC(self.viewModel,password) = [RACSignal merge:@[self.PasswordTextfiled.rac_textSignal,RACObserve(self.PasswordTextfiled, text)]];
    RAC(self.loginButton,enabled) = self.viewModel.validLoginSignal;
    
    [RACObserve(self.loginButton, enabled) subscribeNext:^(id  _Nullable x) {
       
        [x boolValue] ? (self.loginButton.backgroundColor = [UIColor blueColor]) :
        (self.loginButton.backgroundColor = [UIColor grayColor]);
        
    }];
    
    [RACObserve(self.viewModel, aUrlString) subscribeNext:^(id  _Nullable x) {
       
        NSString *aString = (NSString *)x;
        aString.length > 0 ? (self.headerImageView.backgroundColor = [UIColor redColor]): (self.headerImageView.backgroundColor = [UIColor grayColor]);
    }];
    
    //网络请求
    [[[self.loginButton rac_signalForControlEvents:(UIControlEventTouchUpInside)]doNext:^(__kindof UIControl * _Nullable x) {
                //所有视图停止第一响应者
            [self.view endEditing:YES];
            
            [MBProgressHUD showMessage:@"正在登陆" toView:self.view];
        }] subscribeNext:^(__kindof UIControl * _Nullable x) {
            
            [self.viewModel.loginCommand execute:nil];
        }];
    
    [self.viewModel.loginCommand.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
        AppViewController *home = [[AppViewController alloc]init];
        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:home];
        UIWindow *keyWindow = [UIApplication sharedApplication].windows[0];
        keyWindow.rootViewController= nav;
    }];
    
    [self.viewModel.loginCommand.errors subscribeNext:^(NSError * _Nullable x) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [MBProgressHUD showError:x.domain];
    }];
    
    
}

//设置导航栏
-(void)_initNav{
    self.title = @"登录";
    self.view.backgroundColor = [UIColor whiteColor];
}

-(void)_initViews{
    
//    self.view = [[UIImageView alloc]initWithFrame:CGRectZero];
//    [self.view addSubview:self.view];
//    [self.view mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.edges.equalTo(self.view);
//    }];
    
    
    //顶部头像
    self.headerImageView = [[UIImageView alloc]initWithFrame:CGRectZero];
    [self.view addSubview:self.headerImageView];
    [self.headerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.mas_equalTo(100);
        make.size.mas_equalTo(CGSizeMake(80, 80));
    }];
    self.headerImageView.backgroundColor = [UIColor redColor];
    self.headerImageView.layer.cornerRadius = 40.0;
    self.headerImageView.layer.masksToBounds = YES;
    
    
    
    
    //用户名
    self.userNameTextfiled = [[UITextField alloc]initWithFrame:CGRectZero];
    [self.view addSubview:self.userNameTextfiled];

    self.userNameTextfiled.layer.borderWidth = 0.5;
    self.userNameTextfiled.layer.cornerRadius = 6;
    self.userNameTextfiled.layer.masksToBounds = YES;
    self.userNameTextfiled.font = [UIFont systemFontOfSize:16];
    self.userNameTextfiled.borderStyle=UITextBorderStyleRoundedRect;
    self.userNameTextfiled.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.userNameTextfiled.tag = 1001;
    self.userNameTextfiled.userInteractionEnabled = YES;
    [self.userNameTextfiled mas_makeConstraints:^(MASConstraintMaker *make) {
       
    
        
        make.left.mas_equalTo(marginLeftAndRight);
        make.right.mas_equalTo(-marginLeftAndRight);
        make.top.equalTo(self.headerImageView.mas_bottom).offset(60);
        make.height.mas_equalTo(textFiledHeight);
    
    }];
    
    
    self.PasswordTextfiled = [[UITextField alloc]initWithFrame:CGRectZero];
    [self.view addSubview:self.PasswordTextfiled];
    
    self.PasswordTextfiled.borderStyle=UITextBorderStyleRoundedRect;
    self.PasswordTextfiled.clearButtonMode = UITextFieldViewModeWhileEditing;

   self.PasswordTextfiled.layer.borderWidth = 0.5;
   self.PasswordTextfiled.layer.cornerRadius = 5;
   self.PasswordTextfiled.layer.masksToBounds = YES;
    self.PasswordTextfiled.userInteractionEnabled = YES;

 
   self.PasswordTextfiled.secureTextEntry=YES;
   self.PasswordTextfiled.leftViewMode=UITextFieldViewModeAlways;
   self.PasswordTextfiled.tag = 1002;
   
   [self.PasswordTextfiled mas_makeConstraints:^(MASConstraintMaker *make) {
           make.left.mas_equalTo(marginLeftAndRight);
           make.right.mas_equalTo(-marginLeftAndRight);
           make.top.equalTo(self.userNameTextfiled.mas_bottom).offset(15);
           make.height.mas_equalTo(textFiledHeight);
    }];
    
    

                                                                                                                        
    self.loginButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [self.loginButton setTitle:@"登 录" forState:(UIControlStateNormal)];
    self.loginButton.backgroundColor = [UIColor yellowColor];
    self.loginButton.titleLabel.font = [UIFont systemFontOfSize:16];
    self.loginButton.layer.cornerRadius = 5;
    self.loginButton.layer.masksToBounds = YES;
    [self.view addSubview:self.loginButton];
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
    
        make.left.mas_equalTo(marginLeftAndRight);
        make.right.mas_equalTo(-marginLeftAndRight);
        make.top.equalTo(self.PasswordTextfiled.mas_bottom).offset(20);
        make.height.mas_equalTo(textFiledHeight);
        
    }];
    
    
}

-(LoginViewModel *)viewModel{
    
    if (_viewModel == nil) {
        _viewModel = [[LoginViewModel alloc]init];
    }
    return _viewModel;
    
}


@end
